package com.cl.common.exception;

import cn.dev33.satoken.exception.NotLoginException;
import com.cl.common.api.CommonResult;
import com.cl.common.constant.MessageConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.IOException;

/**
 * @Description 全局异常处理
 * @Author cgy
 * @Date 2022/3/13
 * @Version 1.0
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(NullPointerException.class)
    public CommonResult NullPointerException(NullPointerException e) {
        log.info(e.getMessage());
        return CommonResult.fail(MessageConstant.DATA_NULL);
    }

    @ExceptionHandler(NotLoginException.class)
    public CommonResult NullPointerException(NotLoginException e) {
        log.info(e.getMessage());
        return CommonResult.unauthorized();
    }

    @ExceptionHandler(IOException.class)
    public CommonResult IOException(IOException e) {
        log.info(e.getMessage());
        return null;
    }

    @ExceptionHandler(BadSqlGrammarException.class)
    public CommonResult BadSqlGrammarException(BadSqlGrammarException e) {
        log.info(e.getMessage());
        return CommonResult.success();
    }

    @ExceptionHandler(Exception.class)
    public CommonResult myException(Exception e) {
        log.info(e.getMessage());
        return CommonResult.fail(MessageConstant.MYSQL_DATA_FAIL);
    }
}